package com.irdstudio.efp.riskm.service.impl;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.ReflectUtility;
import com.irdstudio.efp.riskm.service.dao.RscAdjustBillDao;
import com.irdstudio.efp.riskm.service.domain.RscAdjustBill;
import com.irdstudio.efp.riskm.service.facade.RscAdjustBillService;
import com.irdstudio.efp.riskm.service.vo.RscAdjustBillVO;

/**
 * <p>ServiceImpl: 风险分类调整借据表				<p>
 * @author chenqm1
 * @date 2018-05-07
 */
@Service("rscAdjustBillService")
public class RscAdjustBillServiceImpl implements RscAdjustBillService, FrameworkService {
	
	private static final List<RscAdjustBillVO> list = null;

	private static Logger logger = LoggerFactory.getLogger(RscAdjustBillServiceImpl.class);

	@Autowired
	private RscAdjustBillDao rscAdjustBillDao;
	
	/**
	 * 新增操作
	 */
	@Override
	public int insertRscAdjustBill(List<RscAdjustBillVO> inRscAdjustBillVo) {
		logger.debug("当前新增数据为:"+ inRscAdjustBillVo.toString());
		int num = 0;
		try {
//			RscAdjustBill rscAdjustBill = new RscAdjustBill();
//			List<RscAdjustBillVO> RscAdjustBillVo = new ArrayList<RscAdjustBillVO>();
//			beanCopy(inRscAdjustBillVo, RscAdjustBillVo);
			num = rscAdjustBillDao.insertRscAdjustBill(inRscAdjustBillVo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(RscAdjustBillVO inRscAdjustBillVo) {
		logger.debug("当前删除数据条件为:"+ inRscAdjustBillVo);
		int num = 0;
		try {
			RscAdjustBill rscAdjustBill = new RscAdjustBill();
			beanCopy(inRscAdjustBillVo, rscAdjustBill);
			num = rscAdjustBillDao.deleteByPk(rscAdjustBill);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscAdjustBillVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(RscAdjustBillVO inRscAdjustBillVo) {
		logger.debug("当前修改数据为:"+ inRscAdjustBillVo.toString());
		int num = 0;
		try {
			RscAdjustBill rscAdjustBill = new RscAdjustBill();
			beanCopy(inRscAdjustBillVo, rscAdjustBill);
			num = rscAdjustBillDao.updateByPk(rscAdjustBill);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscAdjustBillVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	@Override
	public RscAdjustBillVO queryByPk(RscAdjustBillVO inRscAdjustBillVo) {
		
		logger.debug("当前查询参数信息为:"+ inRscAdjustBillVo);
		try {
			RscAdjustBill queryRscAdjustBill = new RscAdjustBill();
			beanCopy(inRscAdjustBillVo, queryRscAdjustBill);
			RscAdjustBill queryRslRscAdjustBill = rscAdjustBillDao.queryByPk(queryRscAdjustBill);
			if (Objects.nonNull(queryRslRscAdjustBill)) {
				RscAdjustBillVO outRscAdjustBillVo = beanCopy(queryRslRscAdjustBill, new RscAdjustBillVO());
				logger.debug("当前查询结果为:"+ outRscAdjustBillVo.toString());
				return outRscAdjustBillVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<RscAdjustBillVO> queryAllOwner(RscAdjustBillVO rscAdjustBillVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscAdjustBillVO> list = null;
		try {
			List<RscAdjustBill> rscAdjustBills = rscAdjustBillDao.queryAllOwnerByPage(rscAdjustBillVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscAdjustBills.size());
			pageSet(rscAdjustBills, rscAdjustBillVo);
			list = (List<RscAdjustBillVO>) beansCopy(rscAdjustBills, RscAdjustBillVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<RscAdjustBillVO> queryAllCurrOrg(RscAdjustBillVO rscAdjustBillVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<RscAdjustBill> rscAdjustBills = rscAdjustBillDao.queryAllCurrOrgByPage(rscAdjustBillVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+rscAdjustBills.size());
		List<RscAdjustBillVO> list = null;
		try {
			pageSet(rscAdjustBills, rscAdjustBillVo);
			list = (List<RscAdjustBillVO>) beansCopy(rscAdjustBills, RscAdjustBillVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<RscAdjustBillVO> queryAllCurrDownOrg(RscAdjustBillVO rscAdjustBillVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<RscAdjustBill> rscAdjustBills = rscAdjustBillDao.queryAllCurrDownOrgByPage(rscAdjustBillVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ rscAdjustBills.size());
		List<RscAdjustBillVO> list = null;
		try {
			pageSet(rscAdjustBills, rscAdjustBillVo);
			list = (List<RscAdjustBillVO>) beansCopy(rscAdjustBills, RscAdjustBillVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	
	//查询用户权限数据——解除风险分类
		@Override
		public List<RscAdjustBillVO> queryRemoveOwner(RscAdjustBillVO rscAdjustBillVO) {
			logger.debug("当前查询参数信息为:"+ rscAdjustBillVO);
			try {
				if(!allFieldsIsNull(rscAdjustBillVO)) {
				List<RscAdjustBill> rscAdjustBill = rscAdjustBillDao.queryRemoveOwnerByPage(rscAdjustBillVO);
				List<RscAdjustBillVO> list = null;
				if (Objects.nonNull(rscAdjustBill)) {
					pageSet(rscAdjustBill, rscAdjustBillVO);
					list = (List<RscAdjustBillVO>) beansCopy(rscAdjustBill, RscAdjustBillVO.class);
					logger.debug("当前查询结果为:"+ list.toString());
					return list;
				} else {
					logger.debug("当前查询结果为空!");
				}
				}
			} catch (Exception e) {
				logger.error("查询数据发生异常!", e);
			}
			return list;
		}

		//查询当前机构权限数据——解除风险分类
		@Override
		public List<RscAdjustBillVO> queryRemoveCurrOrg(RscAdjustBillVO rscAdjustBillVO) {
			logger.debug("当前查询参数信息为:"+ rscAdjustBillVO);
			try {
				if(!allFieldsIsNull(rscAdjustBillVO)) {
				List<RscAdjustBill> rscAdjustBill = rscAdjustBillDao.queryRemoveCurrOrgByPage(rscAdjustBillVO);
				List<RscAdjustBillVO> list = null;
				if (Objects.nonNull(rscAdjustBill)) {
					pageSet(rscAdjustBill, rscAdjustBillVO);
					list = (List<RscAdjustBillVO>) beansCopy(rscAdjustBill, RscAdjustBillVO.class);
					logger.debug("当前查询结果为:"+ list.toString());
					return list;
				} else {
					logger.debug("当前查询结果为空!");
				}
				}
			} catch (Exception e) {
				logger.error("查询数据发生异常!", e);
			}
			return list;
		}

		// 查询当前机构及下属机构权限数据——解除风险分类
		@Override
		public List<RscAdjustBillVO> queryRemoveCurrDownOrg(RscAdjustBillVO rscAdjustBillVO) {
			logger.debug("当前查询参数信息为:"+ rscAdjustBillVO);
			try {
				if(!allFieldsIsNull(rscAdjustBillVO)) {
				List<RscAdjustBill> rscAdjustBill = rscAdjustBillDao.queryRemoveCurrDownOrgByPage(rscAdjustBillVO);
				List<RscAdjustBillVO> list = null;
				if (Objects.nonNull(rscAdjustBill)) {
					pageSet(rscAdjustBill, rscAdjustBillVO);
					list = (List<RscAdjustBillVO>) beansCopy(rscAdjustBill, RscAdjustBillVO.class);
					logger.debug("当前查询结果为:"+ list.toString());
					return list;
				} else {
					logger.debug("当前查询结果为空!");
				}
				}
			} catch (Exception e) {
				logger.error("查询数据发生异常!", e);
			}
			return list;
		}

	//双主键修改
	@Override
	public int updateByTaskNo(RscAdjustBillVO rscAdjustBillVo) {
		logger.debug("当前修改数据为:"+ rscAdjustBillVo.toString());
		int num = 0;
		try {
			RscAdjustBill rscAdjustBill = new RscAdjustBill();
			beanCopy(rscAdjustBillVo, rscAdjustBill);
			num = rscAdjustBillDao.updateByTaskNo(rscAdjustBill);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ rscAdjustBillVo +"修改的数据条数为"+ num);
		return num;
	}
	
	
	//判断前端是否有参数传入，进行条件搜索
		public static boolean allFieldsIsNull(Object obj) {
			if (null == obj)
				return true;
			Field[] fields = obj.getClass().getDeclaredFields();
			Object value;
			for (int i = 0; i < fields.length; i++) {
				try {
					value = ReflectUtility.getObjectFieldValue(obj, fields[i]);
					if ( value == null)
						continue;
					if (value instanceof String ) {
						String s = (String) value;
						if ( "".equals(s.trim()) )	
						 continue;
					}
					if (value instanceof List ) {
						List<?> list = (List<?>) value;
						if (list.size() == 0 )
							continue;
						int num = 0; //空值的次数
						for (Object object : list) {
							if (object == null)
								num++;
							if (object instanceof String ) {
								String s = (String) object;
								if ( "".equals(s.trim()) )	
									num++;
							}
						}
						if ( num == list.size())
							continue;
					}
					if ( value.getClass().isPrimitive() ) 
						continue;
					if ("serialVersionUID".equals(fields[i].getName()))
						continue;
					return false;
				} catch (IllegalArgumentException e) {
				} 
			}
			return true;
		}

	//判断该借据号是否处于已通过
	@Override
	public List<String> queryBillNo() {
		List<String> queryBill= rscAdjustBillDao.queryBillNo();
		return queryBill;
	}
}
